<?php

class UserActivityController extends MainController implements ControllerInterface {

	//o - order
	//v - order vector
	//t - total items to select
	public $tableConfig = array(
		'dict_actions_table' => array(
			'o' => array(
				'id' => 'id_dict_actions',
				'wall' => 'wall',
				'name' => 'name',
				'description' => 'description',
				'_' => 'id'
			),
			'v' => 'malejaco',
			't' => 50
		),
	);

	public function preDispatch($param) {
		$this->RunShared('Auth');
		$this->Run($param);
	}

	public function postDispatch($param) {
		
	}

	public function IndexAction($param) {
		
		$table_data = $this->getTableData('dict_actions_table', $param, array('o' => 'name', 'v' => 'malejaco', 't' => 20));
		$v = $table_data['v'] == 'malejaco' ? 'DESC' : 'ASC';
		$oTable = array(
			'name' => 'dict_actions.name',
			'description' => 'dict_actions.description',
			'id' => 'id_dict_actions'
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona']) ? $param['strona'] : 1;
		
		
		$dalData = DictActionsDAL::GetDalDataObj(
						array(
							'count' => true,
							'tableData' => $this->tableParams['dict_actions_table'],
						)
		);
		
		$this->smarty->assign('c', CategoryDAL::getResult($dalData));

		$dalData->setCount(false);
		$dalData->setLimit((($p - 1) * $table_data['t']) . ', ' . $table_data['t']);

//		$dalData->setOrderBy('category_description ASC');

		$arrData = DictActionsDAL::GetResult($dalData);

		$this->smarty->assign('arrData', $arrData);
	}

	public function EditAction($param) {
		$id = isset($param['id']) ? $param['id'] : -1;

		if (Request::IsPost()) {
			$this->Save($id);
		}

		$dalObj = DictActionsDAL::GetDalDataObj(
						array(
							'tableData' => $this->tableParams['dict_actions_table'],
						)
		);
		$dalObj->addCondition('id_dict_actions', $id);

		$arrData = DictActionsDAL::GetResult($dalObj);

		if (!empty($arrData)) {
			$arrData = $arrData[0];
			$title = 'Edytuj';
			$disabled = 'disabled="disabled"';
		} else {
			$arrData = new DictActionsDAL(-1);
			$title = 'Nowy';
			$disabled = '';
		}
		$this->smarty->assign('disabled', $disabled);
		$this->smarty->assign('title', $title);
		$this->smarty->assign('arrData', $arrData);
	}

	private function Save($id) {
		$arrData = array(
			'name' => Request::GetPost('name'),
			'points' => Request::GetPost('points'),
			'description' => Request::GetPost('description'),
			'wall' => Request::GetPost('wall')
		);

		$errors = $this->ValidateSaveAction(array('validation_data' => $arrData), false);

		if (empty($errors)) {
			$dictActions = new DictActions();
			$dictActions->setId($id);
			$dictActions->setName($arrData['name']);
			$dictActions->setDescription($arrData['description']);
			$dictActions->setPoints($arrData['points']);
			$dictActions->setWall($arrData['wall']);

			DictActionsDAL::Save($dictActions->GetDalDataObj());
			return $this->AddRedirect(Router::GenerateUrl('UserActivityIndex', array('UserActivity' => 'Index')), 0);
		} else {
			$data = array();
			foreach ($errors AS $row) {
				$data[$row['field']] = $row['value'];
				$errors_data[$row['field']] = $row['msg'];
			}
			$this->smarty->assign('arrPostData', $data);
			$this->smarty->assign('errors_data', $errors_data);
		}
	}

	public function ValidateSaveAction($param, $as_action = true) {
		if ($as_action)
			$this->SetValidatorRender();
		$validator = new Validator(isset($param['validation_data']) ? $param['validation_data'] : array());

		$validator->IsEmpty('points', 'Musisz podać wymaganą liczbę punktów');
		$validator->IsNumber('points', 'To musi być liczba całkowita');

		$this->smarty->assign('js_values', $validator->getJsValues());
		$this->smarty->assign('id_form', Request::GetGet('id_f'));
		return $validator->getErrorList();
	}

	public function DeleteAction($param) {
		$this->setNoRender();


//		$obj = DictActionsDAL::GetResult(
//						DictActionsDAL::GetDalDataObj(
//								array(
//									'condition' => array(
//										'id' => array($param['id'])
//									)
//								)
//						)
//		);
//
//		if (!isset($obj[0])) {
//			return $this->AddRedirect(Router::GenerateUrl('UserActivityBadgesIndex', array('UserActivityBadges' => 'Index')), 0);
//		}
//		$obj = $obj[0];
//
//		DictActionsDAL::DefaultDelete($obj->GetDalDataObj());

		$this->AddRedirect(Router::GenerateUrl('UserActivityBadgesIndex', array('UserActivityBadges' => 'Index')), 0);
	}

	public function ToggleWallAction($param) {
		try {
			$this->SetAjaxRender();

			$dalObj = DictActionsDAL::GetDalDataObj(array('id' => $param['id']));
			$this->content = DictActionsDAL::DefaultToggleFieldById($dalObj, 'wall');
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	}

	public function StatusAction($param) {
		$vip = unserialize(array_shift(Config::Get('vip_threshold')));
		$expert = unserialize(array_shift(Config::Get('expert_threshold')));

		if(!is_array($vip))$vip = array();
		if(!is_array($expert))$expert = array();

		$actions = DictActionsDAL::GetResult(DictActionsDAL::GetDalDataObj(array(
			'sortBy' => 'description ASC'
		)));

		$servedActions = array();
		$firstAction = 0;
		foreach($actions AS $action) {
			if($firstAction==0)$firstAction=$action->getId();
			$servedActions[$action->getId()] = $action;
		}

		$this->smarty->assign('actions',$servedActions);
		$this->smarty->assign('firstAction',$firstAction);

		
		if(Request::IsPost()) {

			$data = Request::GetAllPost();

			foreach($data['vip'] AS $k => $v) {
				if($v > 0)$vip[$k] = intval ($v);
				elseif(isset($vip[$k]))unset($vip[$k]);
			}

			foreach($data['expert'] AS $k => $v) {
				if($v > 0)$expert[$k] = intval ($v);
				elseif(isset($expert[$k]))unset($expert[$k]);
			}


			SetupDAL::SaveVariable('vip_threshold',serialize($vip),array_pop(Config::Get('vip_threshold')));
			SetupDAL::SaveVariable('expert_threshold',serialize($expert),array_pop(Config::Get('expert_threshold')));
		}

		$this->smarty->assign('vip',$vip);
		$this->smarty->assign('expert',$expert);
	}

}

?>